package com.klb.service.personal.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.klb.dao.impl.ArticleDaoImpl;
import com.klb.dao.impl.ReplyDaoImpl;
import com.klb.dao.impl.UserDaoImpl;
import com.klb.model.BbsArticle;
import com.klb.model.BbsReply;
import com.klb.model.BbsUser;
import com.klb.service.personal.PersonalServicePersonal;
import com.klb.tools.Tools;

/**
 * Created by fujia on 2016/6/10.
 */
public class PersonalServicePersonalImpl implements PersonalServicePersonal {
    private ArticleDaoImpl articleDao;
    private ReplyDaoImpl replyDao;
    private UserDaoImpl userDao;

    public void setArticleDao(ArticleDaoImpl articleDao) {
        this.articleDao = articleDao;
    }

    public void setReplyDao(ReplyDaoImpl replyDao) {
        this.replyDao = replyDao;
    }

    public void setUserDao(UserDaoImpl userDao) {
        this.userDao = userDao;
    }


    /**
     * 获取用户所有文章与该文章评论
     * */
    @Override
    public List<Map<String, Object>> getArticlesAndRepliesByUserId(int userId) {
        List<Map<String, Object>> myArticles = new ArrayList<>();

        if (!Tools.objectIsNullOrNot(userId)){
            List<BbsArticle> articlesList = this.articleDao.getArticlesByUserId(userId);

            if (!Tools.objectIsNullOrNot(articlesList)){
                for (BbsArticle article : articlesList){
                    List<Map<String, Object>> replyList = this.getReplyMessage(this.replyDao.getReplysByArticleId(article.getArticleId()));
                    Map<String, Object> map = new HashMap<>();
                    map.put("article", article);
                    map.put("replys", replyList);
                    myArticles.add(map);
                }
            }

            return myArticles;
        }

        return null;
    }

    private List<Map<String, Object>> getReplyMessage(List<BbsReply> replys){
        if (!Tools.objectIsNullOrNot(replys)){
            List<Map<String, Object>> replyList = new ArrayList<>();

            for (BbsReply reply : replys){
                BbsUser user = this.userDao.getUserById(reply.getUserId());
                Map<String, Object> map = new HashMap<>();

                map.put("replyId", reply.getReplyId());
                map.put("userId", reply.getUserId());
                map.put("name", user.getName());
                map.put("text", reply.getText());
                map.put("addDate", reply.getDateAdded());

                replyList.add(map);
            }

            return replyList;
        }
        return null;
    }

    /**
     * 删除指定文章
     * */
    @Override
    public boolean deleteArticle(int articleId) {
        boolean result = false;

        if (!Tools.objectIsNullOrNot(articleId)){
            BbsArticle article = this.articleDao.getArticleById(articleId);
            List<BbsReply> replyList = this.replyDao.getReplysByArticleId(articleId);

            if (this.articleDao.deleteQuery(article)){
                result = true;

                for (BbsReply reply : replyList){
                    if (!this.replyDao.deleteQuery(reply)){
                        result = false;
                    }
                }

            }
        }

        return result;
    }



}
